package cn.iocoder.yudao.module.exam.service.studentpaper;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.exam.controller.admin.studentpaper.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.studentpaper.StudentPaperDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.studentpaper.StudentPaperMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.exam.enums.ExamErrorCodeConstants.STUDENT_PAPER_NOT_EXISTS;

/**
 * 试卷管理 Service 实现类
 *
 * @author Francis
 */
@Service
@Validated
public class StudentPaperServiceImpl implements StudentPaperService {

    @Resource
    private StudentPaperMapper studentPaperMapper;

    @Override
    public Long createStudentPaper(StudentPaperSaveReqVO createReqVO) {
        // 插入
        StudentPaperDO studentPaper = BeanUtils.toBean(createReqVO, StudentPaperDO.class);
        studentPaperMapper.insert(studentPaper);

        // 返回
        return studentPaper.getId();
    }

    @Override
    public void updateStudentPaper(StudentPaperSaveReqVO updateReqVO) {
        // 校验存在
        validateStudentPaperExists(updateReqVO.getId());
        // 更新
        StudentPaperDO updateObj = BeanUtils.toBean(updateReqVO, StudentPaperDO.class);
        studentPaperMapper.updateById(updateObj);
    }

    @Override
    public void deleteStudentPaper(Long id) {
        // 校验存在
        validateStudentPaperExists(id);
        // 删除
        studentPaperMapper.deleteById(id);
    }

    @Override
        public void deleteStudentPaperListByIds(List<Long> ids) {
        // 删除
        studentPaperMapper.deleteByIds(ids);
        }


    private void validateStudentPaperExists(Long id) {
        if (studentPaperMapper.selectById(id) == null) {
            throw exception(STUDENT_PAPER_NOT_EXISTS);
        }
    }

    @Override
    public StudentPaperDO getStudentPaper(Long id) {
        return studentPaperMapper.selectById(id);
    }

    @Override
    public PageResult<StudentPaperDO> getStudentPaperPage(StudentPaperPageReqVO pageReqVO) {
        return studentPaperMapper.selectPage(pageReqVO);
    }

}